<?php
/**
 * @ClassName: Test
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2024-09-10 21:36
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Test extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected $statistical_data = '';

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\Member();
        $this->pageTitle = '会员管理';
        $this->enableField = 'status';
        $this->pagesize = 10;
        $this->pk = 'id';

        $this->indexWith = [];

        $this->selectTextField = '{id}#{nickname}({mobile})';
        $this->selectFields = 'id,nickname,mobile';
        $this->selectSearch = 'username|nickname|mobile';
    }

    /**
     * 构建分页数据
     * @return mixed
     */
    protected function buildDataList()
    {
        $page = input('post.__page__/d', 1);
        $page = $page < 1 ? 1 : $page;

        $where = $this->filterWhere();
        $table = $this->table;

        $pagesize = input('post.__pagesize__/d', 0);

        $this->pagesize = $pagesize ?: $this->pagesize;

        $total = $this->dataModel::where($where)->count();
        $data = $this->dataModel::where($where)
            ->order('create_time desc')
            ->limit(($page - 1) * $this->pagesize, $this->pagesize)
            ->select()->toArray();

        // 移除 status 条件
        /*$new_where = [];
        foreach ($where as &$item) {
            if ($item[0] != 'pay_status') {
                $new_where[] = $item;
            }
        }*/

        $total_amount = 0;
        $withdrawn = 0;
        $unwithdrawn = 0;
        $open_withdrawn = 0;
        $open_unwithdrawn = 0;
        $this->statistical_data = "总收入：{$total_amount}，已提现：{$withdrawn}，未提现：{$unwithdrawn}，开户奖已提现：{$open_withdrawn}，开户奖未提现：{$open_unwithdrawn}";

        $this->buildTable($data);
        $table->fill($data);
        $table->paginator($total, $this->pagesize);

        return $data;
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');

        $table->sortable('id, create_time');

        $table->addTop()->display($this->statistical_data)->addStyle("font-size:16px; font-weight: bold; color: #000;");

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnView()
            ->btnDelete();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->post();
//        $data = request()->only([], 'post');

        $result = $this->validate($data, []);

        if (true != $result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}